' This software is a port of Mike Dogan's great ParticleCandy for BlitzBasic3d
'
' http://www.x-pressive.com
'
' Check his page if you're interested in his latest ParticleCandy Engine
' for Corona SDK!
'
' A BIG THANKS goes to him as he gave the permission to publish this as open source
'
' For any support or questions contact: martin.leidel@gmail.com
'
' This software is provided 'as-is', without any express or implied
' warranty.  In no event will the authors be held liable for any damages
' arising from the use of this software.
' 
' Permission is granted to anyone to use this software for any purpose,
' including commercial applications, and to alter it and redistribute it
' freely, subject to the following restrictions:
' 
' 1. The origin of this software must not be misrepresented; you must not
' claim that you wrote the original software. If you use this software
' in a product, an acknowledgment in the product documentation would be
' appreciated but is not required.
' 2. Altered source versions must be plainly marked as such, and must not be
' misrepresented as being the original software.
' 3. This notice may not be removed or altered from any source distribution.

'Press:
'A = 4000 PARTICLES"
'B = STONE SMASH"
'C = EXPLOSION I"
'D = EXPLOSION II"
'E = SMOKE"
'F = TORCH"
'G = ELECTRIC SPARKLES"
'H = FLAME THROWER"
'I = WATERFALL"
'J = FONTAIN"
'K = PARTICLE TRAILS"
'SPACE = Stop all

Import particlecandy
Import particletypes
Import mojo
Import minib3d

Function Main()
  New ParticleDemo()
End Function

Class ParticleDemo Extends App
  'FPS stuff
  Field _loopTimeAll:Int
  Field _loopTimeStart:Int
  Field _loopTimeEnd:Int
  Field _FPSCounter:Int
  Field _FPSLastUpdate:Int
  Field _renderTime:Float
  Field _renderTimeAll:Int
  Field _FPS:Float

  Field _cam:TCamera
  Field _camPiv:TEntity
  Field _light:TLight
  Field _isInitialized:Bool = False
  Field _em0:Emitter, _em1:Emitter, _em2:Emitter, _em3:Emitter, _em4:Emitter, _em5:Emitter
  Field _em6:Emitter, _em7:Emitter, _em8:Emitter, _em9:Emitter, _em10:Emitter
  Field _txt:TText
  
  Field _pc:ParticleCandy
  
  Method OnCreate()
    SetRender()
    Init()
    SetUpdateRate( 30 )
  End Method
  
  Method OnSuspend:Int()
    Return 0
  End Method
  
  Method OnResume:Int()
    Return 0
  End Method

  Method OnUpdate()
    If( Not _isInitialized ) Return
    
    TurnEntity( _em7._piv, 0, 0, 0.35 )

    If( KeyHit( KEY_ESCAPE ) )
      _pc.free()
      Error ""
    End If

  	If( KeyHit( KEY_SPACE ) )
        _pc.clearParticles()
    End If
    
  	If( KeyHit( KEY_A ) )
      _pc.clearParticles()
      _em0.start()
  	End If

  	If( KeyHit( KEY_B ) )
      If( _em0.isActive() ) _pc.clearParticles()
      _em1.start()
  	End If
  
  	If( KeyHit( KEY_C ) )
      PositionEntity( _em2._piv, Rnd( -5, 5 ), Rnd( -5, 5 ), 0 )
      If( _em0.isActive() ) _pc.clearParticles()
      _em2.start()
  	End If
  
  	If( KeyHit( KEY_D ) )
      PositionEntity( _em3._piv, Rnd( -5, 5 ), Rnd( -5, 5 ), 0 )
      If( _em0.isActive() ) _pc.clearParticles()
      _em3.start()
  	End If
  
  	If( KeyHit( KEY_E ) )
      If( _em0.isActive() ) _pc.clearParticles()
      _em4.start()
  	End If
  
  	If( KeyHit( KEY_F ) )
      If( _em0.isActive() ) _pc.clearParticles()
      _em5.start()
  	End If
  
  	If( KeyHit( KEY_G ) )
      If( _em0.isActive() ) _pc.clearParticles()
      _em6.start()
  	End If
  
  	If( KeyHit( KEY_H ) )
      If( _em0.isActive() ) _pc.clearParticles()
      _em7.start()
  	End If
  
  	If( KeyHit( KEY_I ) )
      If( _em0.isActive() ) _pc.clearParticles()
      _em8.start()
  	End If
  
  	If( KeyHit( KEY_J ) )
      If( _em0.isActive() ) _pc.clearParticles()
      _em9.start()
  	End If
  
  	If( KeyHit( KEY_K ) )
      If( _em0.isActive() ) _pc.clearParticles()
      _em10.start()
  	End If
    
    '_txt.SetMode2D()
    '_txt.SetText( "FPS: " + _FPS, 0, 10 )
    UpdateWorld()
  End Method
  
  Method OnRender()
    If( Not _isInitialized ) Return

    _loopTimeStart = Millisecs()

    _pc.render()
    RenderWorld()

    _loopTimeEnd = Millisecs()
    updateFPS()
  End Method


  Method Init()
    If( _isInitialized ) Return
    
    SetRender()

    _camPiv = CreatePivot()
    _cam = CreateCamera( _camPiv )
    MoveEntity( _cam, 0, 0, -15 )
    
    _txt = TText.CreateText( _cam )

    _light = CreateLight()
    'Listener = xCreateListener(Cam, .005, 1, 1)
    
    _pc = New ParticleCandy()
    _pc.init( _cam, "monkey://data/particles.png", 3 )
    _pc.setParticleUpdateCycle( 2 )

    _em0 = CreateEmitter_Test( _pc )
    _em1 = CreateEmitter_Rocks( _pc )
    _em2 = CreateEmitter_Explosion1( _pc )
    _em3 = CreateEmitter_Explosion2( _pc )
    _em4 = CreateEmitter_Smoke( _pc )
    _em5 = CreateEmitter_Torch( _pc )
    _em6 = CreateEmitter_Sparks( _pc )
    _em7 = CreateEmitter_FlameThrower( _pc )
    _em8 = CreateEmitter_Waterfall( _pc )
    _em9 = CreateEmitter_WaterFontain( _pc )
    _em10 = CreateEmitter_SmokeFlares( _pc )

    PositionEntity( _em4._piv, 0, -7, 0 )
    PositionEntity( _em5._piv, 0, -7, 0 )
    RotateEntity  ( _em9._piv, 0, 0, 45 )
    _em2.setScale( 0.75 )
    _em3.setScale( 0.75 )
    _em6.setScale( 1.5 )
    
    _isInitialized = True
  End Method

  Method updateFPS:Void()
    _loopTimeAll = _loopTimeEnd - _loopTimeStart
    _renderTimeAll += _loopTimeAll
      
    _FPSCounter += 1
    If( ( _loopTimeStart - _FPSLastUpdate ) > 500 )
      _renderTime = Float( _renderTimeAll ) / Float( _FPSCounter )
      If( _renderTime < 1.0 ) Then _renderTime = 1.0
      _renderTimeAll = 0
      _FPSLastUpdate = _loopTimeStart
      _FPS = 1000.0 / _renderTime
      _FPSCounter = 0
    End If
  End Method
End Class

'	Text 5,0, "FRAMES: " + totalfps + " PARTICLES: " + pt_numParticles + " PARTICLES RENDERED: " + pt_particlesRendered
'	If showText
'		y = 20 : Text 5,20,"A = 4000 PARTICLES"
'		y = y + 15 : Text 5,y,"B = STONE SMASH"
'		y = y + 15 : Text 5,y,"C = EXPLOSION I"
'		y = y + 15 : Text 5,y,"D = EXPLOSION II"
'		y = y + 15 : Text 5,y,"E = SMOKE"
'		y = y + 15 : Text 5,y,"F = TORCH"
'		y = y + 15 : Text 5,y,"G = ELECTRIC SPARKLES"
'		y = y + 15 : Text 5,y,"H = FLAME THROWER"
'		y = y + 15 : Text 5,y,"I = WATERFALL"
'		y = y + 15 : Text 5,y,"J = FONTAIN"
'		y = y + 15 : Text 5,y,"K = PARTICLE TRAILS"
'		y = y + 15 : Text 5,y,"SPACE = TEXT ON/OFF (FASTER!)"
'	End If
